<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/royal') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },

  },
  data() {
    return {
      chart: null,
      opinion:{
        // title: {
        //   text: '情感分析'
        // },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['舆情数量']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['2020-09-01', '2020-09-02', '2020-09-03','2020-09-04', '2020-09-05', '2020-09-06','2020-09-07', '2020-09-08', '2020-09-09','2020-09-10', '2020-09-11', '2020-09-12',
            '2020-09-13', '2020-09-14', '2020-09-15', '2020-09-16','2020-09-17', '2020-09-18', '2020-09-19','2020-09-20', '2020-09-21', '2020-09-22',
            '2020-09-23', '2020-09-24', '2020-09-25', '2020-09-26','2020-09-27', '2020-09-28', '2020-09-29','2020-09-30']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'line',
            data: [120, 132, 101, 134, 90, 230, 210,34,234,45,13,45,65,231,453,56,33,22,12,55,96,123,25,143,234,543,345,234,123]
          }
        ]
      }
    }
  },
  watch:{

  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'royal')
      this.chart.setOption(this.opinion)
    }
  }
}
</script>
